package com.ice.work.moment.crawler.service.impl;

import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.constant.CommonSystemConstant;
import com.ice.work.moment.crawler.dao.comment.CommentRepository;
import com.ice.work.moment.crawler.service.ICommentService;
import com.ice.work.moment.facade.crawler.model.Comment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Objects;

/**
 * @Description 股票评论信息
 * @Author DELL
 * @Create 2019-05-05
 * @Since 1.0.0
 */
@Service
@Slf4j
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public Boolean saveOrUpdate(Collection<Comment> comments) throws BaseBizException {
        Assert.notEmpty(comments, "comments not null");
        return commentRepository.saveOrUpdateBatch(comments, CommonSystemConstant.BATCH_SIZE);
    }

    @Override
    public Boolean save(Comment comment) throws BaseBizException {
        Assert.notNull(comment, "comment not null");
        return commentRepository.save(comment);
    }

    @Override
    public Boolean saveOrUpdate(Comment comment) throws BaseBizException {
        Assert.notNull(comment, "comment not null");
        Comment exists = commentRepository.findByShareIdAndCommentAtAndDataStatus(comment);
        if (Objects.nonNull(exists)) {
            comment.setId(exists.getId());
        }
        return commentRepository.saveOrUpdate(comment);
    }
}
